import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/components/base_components/base_filter_select_chip.dart';
import 'package:x3_component_core/components/selector_components/base_filter_selector.dart';
import 'package:x3_component_core/models/base_advanced_filter_model.dart';
import 'package:x3_component_core/utils/base_component_utils.dart';

class BaseFilterInputDateRange extends StatefulWidget {
  final AdvancedFilterModel model;
  final Map<String, dynamic> dataSource;
  final List<Map<String, dynamic>> dateRangeList;
  const BaseFilterInputDateRange({Key key, @required this.model, @required this.dataSource, @required this.dateRangeList})
      : assert(model != null),
        assert(dataSource != null),
        super(key: key);
  @override
  _BaseFilterInputDateRangeState createState() => _BaseFilterInputDateRangeState();
}

class _BaseFilterInputDateRangeState extends State<BaseFilterInputDateRange> {
  AdvancedFilterModel get model => widget.model;
  Map<String, dynamic> get _dataSource => widget.dataSource;
  List<Map<String, dynamic>> get _dateRangeList => widget.dateRangeList;
  List<String> _currentDate = [];
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    /// 判断是否不限
    bool isAll = _dataSource[model.tagId] == null ? true : false;
    if (!isAll) _currentDate = [_dataSource[model.tagId]['beginDate'], _dataSource[model.tagId]['endDate']];

    /// 显示筛选文本
    String _selectedText = isAll == true ? '不限' : _currentDate.join(' ~ ');
    List<Widget> _children = [];
    for (Map item in _dateRangeList) {
      _children.add(BaseFilterSelectChip(
        selected: _onIsSelected(item),
        label: item['label'],
        onSelected: (bool) {
          _onSelected(item);
        },
      ));
    }
    return BaseFilterSelector(
      title: model.tagName,
      selectedText: _selectedText,
      children: _children,
      isExpanded: model.tagIsExpanded,
      onChange: (bool state) {
        model.tagIsExpanded = state;
      },
    );
  }

  void _onSelected(Map item) {
    switch (item['type']) {
      case 'all':
        setState(() {
          _dataSource[model.tagId] = null;
        });
        break;
      case 'custom':
        DateTimeRange _initialDateRange;
        if (_dataSource[model.tagId] != null) {
          String _beginDate = _dataSource[model.tagId]['beginDate'];
          String _endDate = _dataSource[model.tagId]['endDate'];
          DateTime beginDate = DateTime.parse(_beginDate).toLocal();
          DateTime endDate = DateTime.parse(_endDate).toLocal();
          _initialDateRange = DateTimeRange(start: beginDate, end: endDate);
        }
        BaseComponentUtils.openDateRangePicker(context, initialDateRange: _initialDateRange).then((value) {
          if (value != null) {
            String beginDate = BaseTransformUtil.stringToDate(value.start.toString());
            String endDate = BaseTransformUtil.stringToDate(value.end.toString());
            _setInputDateRange(beginDate: beginDate, endDate: endDate);
            _dateRangeList.last['value'] = [beginDate, endDate];
          }
        });
        break;
      default:
        List _range = item['value'] as List;
        _setInputDateRange(beginDate: _range.first, endDate: _range.last);
    }
  }

  void _setInputDateRange({@required String beginDate, @required String endDate}) {
    setState(() {
      _dataSource[model.tagId] = {"dayType": 0, "beginDate": beginDate, "endDate": endDate, "days": null};
    });
  }

  bool _onIsSelected(Map item) {
    if (_dataSource[model.tagId] == item['value']) return true;
    return item['value'].toString() == _currentDate.toString();
  }
}
